package org.example.dao;

import org.example.model.Article;
import org.example.util.DBUtil;
import org.junit.Assert;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class ArticleDao {

    //根据用户查询关联的文章列表
    public static List<Article> queryBlogList(Integer userId) {
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            //1.获取连接
            c = DBUtil.getConnection();
            String sql = "select * from article where user_id=?";
            //2.创建操作命令对象
            ps = c.prepareStatement(sql);
            //替换占位符
            ps.setInt(1, userId);
            //3.执行sql
            rs = ps.executeQuery();
            //4.处理结果集
            List<Article> articles = new ArrayList<>();
            while (rs.next()){
                //每一行数据，都是一个Article对象、
                Article a = new Article();
                a.setId(rs.getInt("id"));
                a.setTitle(rs.getString("title"));
                a.setContent(rs.getString("content"));
                java.sql.Timestamp createTime = rs.getTimestamp("create_time");
                a.setCreateTime(new java.util.Date(createTime.getTime()));
                a.setUserId(userId);
                articles.add(a);
            }
            return articles;
        }catch (Exception e){
            throw new RuntimeException("查询博客列表出错", e);
        }finally {
            DBUtil.close(c, ps, rs);
        }
    }

    public static Article queryBlogDetail(int id) {
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            //1.获取连接
            c = DBUtil.getConnection();
            String sql = "select * from article where id=?";
            //2.创建操作命令对象
            ps = c.prepareStatement(sql);
            //替换占位符
            ps.setInt(1, id);
            //3.执行sql
            rs = ps.executeQuery();
            //4.处理结果集
            while (rs.next()){
                //每一行数据，都是一个Article对象、
                Article a = new Article();
                a.setId(rs.getInt("id"));
                a.setTitle(rs.getString("title"));
                a.setContent(rs.getString("content"));
                java.sql.Timestamp createTime = rs.getTimestamp("create_time");
                a.setCreateTime(new java.util.Date(createTime.getTime()));
                a.setUserId(rs.getInt("user_id"));
                return a;
            }
            return null;
        }catch (Exception e){
            throw new RuntimeException("查询博客详情出错", e);
        }finally {
            DBUtil.close(c, ps, rs);
        }
    }

    public static int insert(Article a) {
        Connection c = null;
        PreparedStatement ps = null;
        try{
            //1.获取连接
            c = DBUtil.getConnection();
            String sql = "insert into article(title, content, user_id, create_time) values(?,?,?,?)";
            //2.创建操作命令对象
            ps = c.prepareStatement(sql);
            //替换占位符
            ps.setString(1, a.getTitle());
            ps.setString(2, a.getContent());
            ps.setInt(3, a.getUserId());
            ps.setTimestamp(4,new Timestamp(System.currentTimeMillis()));
            return ps.executeUpdate();
        }catch (Exception e){
            throw new RuntimeException("发布文章失败", e);
        }finally {
            DBUtil.close(c, ps);
        }
    }

//    @Test
//    public void testQueryBlogList(){
//        List<Article> articles = queryBlogList(1);
//        System.out.println(articles);
//        Assert.assertTrue();
//    }

    @Test
    public void testQueryBlogDetail(){
        Article a = queryBlogDetail(1);
        System.out.println(a);
    }

    @Test
    public void testInsert(){
        Article a = new Article();
        a.setTitle("单元测试-标题");
        a.setContent("单元测试-内容");
        a.setUserId(1);
        int n = insert(a);
        Assert.assertTrue(n == 1);
    }
}
